package com.boguan.device.service.impl;

import com.boguan.common.annotation.DataScope;
import com.boguan.common.core.domain.entity.SysUser;
import com.boguan.common.utils.StringUtils;
import com.boguan.device.domain.DeviceAlarmLog;
import com.boguan.device.domain.DeviceLog;
import com.boguan.device.domain.vo.DeviceDetailAlarmVo;
import com.boguan.device.mapper.DeviceAlarmLogMapper;
import com.boguan.device.mapper.DeviceLogMapper;
import com.boguan.device.mapper.DeviceMapper;
import com.boguan.device.service.IDeviceAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 设备报警信息逻辑处理实现类
 *
 * @author chen
 */
@Service
public class DeviceAlarmServiceImpl implements IDeviceAlarmService {

    @Autowired
    private DeviceAlarmLogMapper deviceAlarmLogMapper;

    @Autowired
    private DeviceLogMapper deviceLogMapper;


    @Autowired
    DeviceMapper deviceMapper;

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public List<DeviceAlarmLog> alarmMessage(DeviceAlarmLog alarmLog) {
        return deviceAlarmLogMapper.searchPage(alarmLog);
    }

    @Override
    public DeviceAlarmLog alarmMessageDetails(Long id) {
        return deviceAlarmLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(DeviceAlarmLog alarmLog) {

        return deviceAlarmLogMapper.updateByPrimaryKeySelective(alarmLog);
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public List<DeviceLog> faultMessage(DeviceLog deviceLog) {
        return deviceLogMapper.faultMessage(deviceLog);
    }

    @Override
    public DeviceLog faultMessageDetails(Long id) {
        return deviceLogMapper.selectByPrimaryKey(id);
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public Map<String, Integer> alarmNumber(SysUser user) {
        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("early_warning", 0);
        resultMap.put("serious_warning", 0);
        resultMap.put("alarm", 0);
        resultMap.put("fault", 0);
        resultMap.put("untreated", 0);
        List<Map<String, Object>> maps = deviceAlarmLogMapper.alarmNumber(user);
        if (StringUtils.isNotNull(maps)) {
            for (Map<String, Object> map : maps) {
                Integer key = (Integer) map.get("alarmLevel");
                String newKey = key == null ? null : key == 1 ? "early_warning" : key == 2 ? "serious_warning" : key == 3 ? "alarm" : "fault";
                if (newKey != null) {
                    resultMap.put(newKey, Math.toIntExact((Long) map.get("number")));
                }
            }
        }
        //查询出未处理的数据
        Integer count = deviceAlarmLogMapper.untreatedAlarmNumber(user);
        if (StringUtils.isNotNull(count)) {
            resultMap.put("untreated", count);
        }
        return resultMap;
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public List<Map<String, Object>> analysisAlarmNumber(SysUser user) {
        List<Map<String, Object>> maps = deviceAlarmLogMapper.analysisAlarmNumber(user);
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Map<String, Object>> timeMap = new HashMap<>();
        String timeStr = "";
        for (Map<String, Object> map : maps) {
            String alarmTime = (String) map.get("alarmTime");
            Map<String, Object> dateMap = timeMap.get(alarmTime);
            Long number = (Long) map.get("number");
            if (!timeStr.equals("") && !timeStr.equals(alarmTime)) {
                resultList.add(timeMap.get(timeStr));

            }
            timeStr = alarmTime;
            if (StringUtils.isNull(dateMap)) {
                dateMap = new HashMap<>();
                dateMap.put("early_warning", 0L);
                dateMap.put("serious_warning", 0L);
                dateMap.put("alarm", 0L);
                dateMap.put("total", 0L);
            }
            Integer key = (Integer) map.get("alarmLevel");
            String newKey = key == 1 ? "early_warning" : key == 2 ? "serious_warning" : "alarm";
            dateMap.put(newKey, number);
            Long total = (Long) dateMap.get("total");
            total += number;
            dateMap.put("total", total);
            dateMap.put("time", timeStr);
            timeMap.put(timeStr, dateMap);
        }
        if (!timeStr.equals("")) {
            resultList.add(timeMap.get(timeStr));
        }
        return resultList;
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public Integer selectAlarmCount(SysUser user) {
        return deviceAlarmLogMapper.selectAlarmCount(user);
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public List<Map<String, Integer>> untreatedAlarmCount(SysUser user) {
        return deviceAlarmLogMapper.untreatedAlarmCount(user);
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public List<Map<String, Object>> alarmCountTrend(SysUser user) {
        return deviceAlarmLogMapper.alarmCountTrend(user);
    }

    @Override
    @DataScope(deptAlias = "sd", userAlias = "su")
    public List<Map<String, Integer>> alarmLevelCount(SysUser user) {
        return deviceAlarmLogMapper.alarmLevelCount(user);
    }

    @Override
    public DeviceAlarmLog selectAlarmByError(Long deviceId, String eventName, Integer errorStatus, Integer status) {
        return deviceAlarmLogMapper.selectAlarmByError(deviceId, eventName, errorStatus, status);
    }

    @Override
    public int insertSelective(DeviceAlarmLog deviceAlarmLogs) {
        return deviceAlarmLogMapper.insertSelective(deviceAlarmLogs);
    }


    @Override
    public int deleteAlarm(Long id) {
        return deviceAlarmLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int selectAlarmCountByDeviceId(Long deviceId, String identifier) {
        return deviceAlarmLogMapper.selectAlarmCountByDeviceId(deviceId, identifier);
    }

    @Override
    public List<DeviceDetailAlarmVo> deviceAlarmManage(String deviceNo) {
        return deviceMapper.deviceAlarmManage(deviceNo,null,null);
    }

    @Override
    public List<DeviceDetailAlarmVo> deviceAlarmManageDetail(String deviceId,String alarmTime) {
        return deviceMapper.deviceAlarmManageDetail(deviceId, alarmTime);
    }

    @Override
    public List<DeviceDetailAlarmVo> deviceAlarmSearch( String device_num,String device_name, String status) {
        return deviceMapper.deviceAlarmManage(device_num, device_name, status);
    }


}
